package main.flex.component.media
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import main.flex.component.media.containers.CoverFlowContainer;
	
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.VideoDisplay;
	import mx.core.UIComponent;
	import mx.core.mx_internal;
	import mx.events.FlexEvent;
	
	import org.servebox.commons.utils.ArrayUtils;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	use namespace mx_internal;
	
	public class MediaCoverFlowContainer extends CoverFlowContainer
	{
		public function MediaCoverFlowContainer()
		{
			super();
			addEventListener("updateMedia",stopVideo);
		}
		
		private var _mediaData			: Array = new Array();
		private var _affichage			: UIComponent;
		
		[Bindable(event="mediaDataChanged")]
		public function get mediaData ( ) : Array
		{
			return _mediaData;
		}
		
		public function set mediaData ( val : Array ) : void
		{
			_mediaData = ArrayUtils.arrayCopy( val );
			refreshDisplay();
			dispatchEvent( new Event( "mediaDataChanged" ) );
		}
		
		public function isVideo ( pStr : String ) : Boolean
		{
			invalidateDisplayList();
			return pStr.substr(pStr.length-3)=="flv";
		}
		
		protected function refreshDisplay():void
		{
			super.commitProperties();
			if ( numChildren > 0 )
				removeAllChildren();
			if ( mediaData.length > 0 )
				for ( var i : uint = 0; i < mediaData.length ; i++ )
				{
					var cheminRelatif:String=mediaData[ i ].chemin.toString().substring(4);
					if(isVideo( mediaData[ i ].chemin ) )
					{	
						_affichage=new VideoDisplay();
						VideoDisplay(_affichage).autoPlay=false;
						VideoDisplay(_affichage).live=false;
						VideoDisplay(_affichage).load();
						VideoDisplay(_affichage).autoBandWidthDetection=true;
						VideoDisplay(_affichage).source=cheminRelatif;
						VideoDisplay(_affichage).addEventListener(MouseEvent.CLICK,playVideo);
					}else{
						_affichage=new Image();
						Image(_affichage).source=cheminRelatif;
					}
					var panel : Panel = new Panel();
					panel.width = 350;
					panel.height = 300;
					panel.horizontalScrollPolicy="off";
					panel.verticalScrollPolicy="off";
					panel.setStyle("headerHeight",10);
					_affichage.height=panel.height-21;
					_affichage.width=panel.width-20;
					panel.addChild(_affichage);
					addChild( panel );
					_affichage.addEventListener(FlexEvent.CREATION_COMPLETE,createComplete);
				}
		}
		
		private function createComplete(pEvt:FlexEvent):void
		{
			invalidateDisplayList()
			//layoutChildren(pEvt.currentTarget.width, pEvt.currentTarget.height);
		}		
		
		private function playVideo(pEvt:MouseEvent):void
		{
			if(!pEvt.currentTarget.playing)
			{
				pEvt.currentTarget.play();
			}else{
				pEvt.currentTarget.pause();
			}	
		}
		
		private function stopVideo(pEvt:Event):void
		{
			/* var lastChild : DisplayObject = this.getChildAt( lastChildId );
			if ( DisplayObjectContainer( lastChild ).getChildAt( 0 ) && DisplayObjectContainer( lastChild ).getChildAt( 0 ) is VideoDisplay )
				VideoDisplay( DisplayObjectContainer( lastChild ).getChildAt( 0 ) ).stop();
			lastChildId = selectedIndex */;
			
		}
	}
}